package com.yuebook.smartrpc.framework.spring;

import com.yuebook.smartrpc.framework.invoker.InvokerFactoryBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

/**
 * @author honglei
 * @date 2018/11/19 下午4:20
 */
public class InvokerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    @Override
    protected Class<?> getBeanClass(Element element) {
        return InvokerFactoryBean.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        try {
            String tartgetItf = element.getAttribute("interface");
            long timeout = Long.parseLong(element.getAttribute("timeout"));
            String remoteAppkey = element.getAttribute("remoteAppKey");
            String groupName = element.getAttribute("groupName");
            builder.addPropertyValue("targetItf", tartgetItf);
            builder.addPropertyValue("timeout", timeout);
            builder.addPropertyValue("remoteAppKey", remoteAppkey);
            if (StringUtils.isNotBlank(groupName)) {
                builder.addPropertyValue("groupName", groupName);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
